/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : NumberComparisonProcessHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 1, 2008       Raghu Sarraff			       Created
 */
package net.lwf.handler.math.number;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.number.util.NumberFormatter;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

/**
 * Type the class description here
 *  
 */
public class NumberComparisonProcessHandler extends ProcessHandler implements
IProcessHandler {

	public DisplayFormatter getDisplayFormatter() {
		
		return null;
	}

	public String getQuestionDesc() {
		
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		
		return this.generateSingle(false);
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		int nQuestions = getRows() * getColumns();
		for(int i=0; i<nQuestions; i++) {
			QuestionInfo question = generateSingle(true);
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
		
		
	}

	@Override
	public int getRows() {
		
		return 0;
	}

	@Override
	public int getColumns() {
		
		return 0;
	}

	@Override
	public boolean isPortrait() {
		
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		
		return null;
	}

	@Override
	public String getPdfNote() {
		
		return null;
	}
	
	private QuestionInfo generateSingle(boolean isMultiple) {
		
		QuestionInfo qinfo = new QuestionInfo();
		int digits = RandomNumberGenerator.generate(1, 5);
		int[] data = {RandomNumberGenerator.generate(digits), RandomNumberGenerator.generate(digits)};
		qinfo.setQuestionObject(data);
		if(data[0] > data[1]) {
			qinfo.setAnswer(ICommonConstant.GREATER_THAN);
		}
		else if(data[0] < data[1]) {
			qinfo.setAnswer(ICommonConstant.LESS_THAN);
		}
		else if(data[0] == data[1]) {
			qinfo.setAnswer(ICommonConstant.EQUAL_TO);
		}
		
		qinfo.setPlaceHolder("Compare the following numbers."); //Instructional text to the UI

		if(!isMultiple) {
			//Set HTML question
			qinfo.setHtmlQuestion(this.generateHtmlQuestion(data));
			//Set radio answers
			qinfo.setRadioAnswerChoices(this.getRadioAnswers());			
		}
		
		return qinfo;
	}
	
	private String generateHtmlQuestion(int[] data) {
		StringBuilder sbQBlock = new StringBuilder();
		sbQBlock.append("<table border=\"0\" align=\"center\" >");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td class=\"data\" >");
					sbQBlock.append(NumberFormatter.format(String.valueOf(data[0])));
				sbQBlock.append("</td>");
				sbQBlock.append("<td> &nbsp; </td>");
				sbQBlock.append("<td class=\"question\" > ? </td>");
				sbQBlock.append("<td> &nbsp; </td>");
				sbQBlock.append("<td class=\"data\" >");
					sbQBlock.append(NumberFormatter.format(String.valueOf(data[1])));
				sbQBlock.append("</td>");				
        	sbQBlock.append("</tr>");
        sbQBlock.append("</table>");
		return sbQBlock.toString();	
	}
	
	private List<RadioData> getRadioAnswers() {
		List<RadioData> rdata = new ArrayList<RadioData>();
		
		rdata.add(new RadioData(ICommonConstant.GREATER_THAN, ICommonConstant.GREATER_THAN));
		rdata.add(new RadioData(ICommonConstant.LESS_THAN, ICommonConstant.LESS_THAN));
		rdata.add(new RadioData(ICommonConstant.EQUAL_TO, ICommonConstant.EQUAL_TO));
		
		Collections.shuffle(rdata);
		
		return rdata;
	}
	
} //NumberComparisonProcessHandler


